/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clusteranalyst2;
import java.text.DecimalFormat;
/**
 *
 * @author Administrator
 */
public class CaclAbsDistance {

    
    public static double  DistanceBetween(double[] class1,double [] class2) throws Exception
    {
         DecimalFormat df=new DecimalFormat("0.00");
        if(class1.length==class2.length)
        {
            double ClassesDistance=0;
            for(int m=0;m<class1.length;m++)
            {
                ClassesDistance+=Math.abs(class1[m]-class2[m]);
            }
            return Double.parseDouble(df.format(ClassesDistance));
        }
        else
        { 
            throw new Exception("类与类之间的长度必须一致");
        }      
    }
//绝对距离的计算
    public double[][] getAbsDistance()
    {
        //获取标准化后的数据矩阵
        DataPre dp=new DataPre();
        double [][] dataPrepared=dp.range_standard();
        //距离计算后的数据放于 Distance矩阵中
        double [][] Distance=new double[dataPrepared.length][dataPrepared.length]; 
        for(int i=0;i<dataPrepared.length;i++)
        {            
            for(int j=0;j<dataPrepared.length;j++)
            {
                try
                {
                    Distance[i][j]=DistanceBetween(dataPrepared[i],dataPrepared[j]);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }               
            }
        }
        /*
        System.out.println("距离矩阵");
        PrintMethods pm=new  PrintMethods();
        pm.printMatrix(Distance);
         System.out.println("****************************************");
         * 
         */
        return Distance;
    }
}
